package o;

import android.media.AudioTrack;
import android.os.SystemClock;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import java.lang.reflect.Method;

public final class ach {
    private long ʻ;
    private int ʻॱ;
    private boolean ʼ;
    private long ʼॱ;
    @Nullable
    private acg ʽ;
    private int ʽॱ;
    private long ʾ;
    private long ʿ;
    private long ˈ;
    private int ˊ;
    private boolean ˊॱ;
    private int ˋ;
    private long ˋॱ;
    private final d ˎ;
    private final long[] ˏ;
    private long ˏॱ;
    private boolean ͺ;
    @Nullable
    private AudioTrack ॱ;
    @Nullable
    private Method ॱˊ;
    private long ॱˋ;
    private long ॱˎ;
    private int ॱॱ;
    private long ॱᐝ;
    private long ᐝ;
    private long ᐝॱ;

    public interface d {
        void ˋ(long j, long j2, long j3, long j4);

        void ˏ(long j);

        void ॱ(int i, long j);

        void ॱ(long j, long j2, long j3, long j4);
    }

    public ach(d dVar) {
        this.ˎ = (d) amm.ॱ(dVar);
        if (anr.ˋ >= 18) {
            try {
                this.ॱˊ = AudioTrack.class.getMethod("getLatency", (Class[]) null);
            } catch (NoSuchMethodException e) {
            }
        }
        this.ˏ = new long[10];
    }

    public void ˋ(AudioTrack audioTrack, int i, int i2, int i3) {
        this.ॱ = audioTrack;
        this.ˋ = i2;
        this.ˊ = i3;
        this.ʽ = new acg(audioTrack);
        this.ॱॱ = audioTrack.getSampleRate();
        this.ʼ = ˊ(i);
        this.ˊॱ = anr.ˋ(i);
        this.ᐝ = this.ˊॱ ? ʽ((long) (i3 / i2)) : -9223372036854775807L;
        this.ᐝॱ = 0;
        this.ॱˋ = 0;
        this.ॱᐝ = 0;
        this.ͺ = false;
        this.ˈ = -9223372036854775807L;
        this.ʼॱ = -9223372036854775807L;
        this.ˏॱ = 0;
    }

    public long ˋ(boolean z) {
        if (((AudioTrack) amm.ॱ(this.ॱ)).getPlayState() == 3) {
            ˎ();
        }
        long nanoTime = System.nanoTime() / 1000;
        acg o_acg = (acg) amm.ॱ(this.ʽ);
        if (o_acg.ˏ()) {
            long ʽ = ʽ(o_acg.ʽ());
            if (o_acg.ॱ()) {
                return (nanoTime - o_acg.ʼ()) + ʽ;
            }
            return ʽ;
        }
        long ʼ;
        if (this.ʽॱ == 0) {
            ʼ = ʼ();
        } else {
            ʼ = this.ʻ + nanoTime;
        }
        if (z) {
            return ʼ;
        }
        return ʼ - this.ˏॱ;
    }

    public void ˏ() {
        ((acg) amm.ॱ(this.ʽ)).ˋ();
    }

    public boolean ˋ() {
        return ((AudioTrack) amm.ॱ(this.ॱ)).getPlayState() == 3;
    }

    public boolean ˏ(long j) {
        int playState = ((AudioTrack) amm.ॱ(this.ॱ)).getPlayState();
        if (this.ʼ) {
            if (playState == 2) {
                this.ͺ = false;
                return false;
            } else if (playState == 1 && ᐝ() == 0) {
                return false;
            }
        }
        boolean z = this.ͺ;
        this.ͺ = ˊ(j);
        if (!(!z || this.ͺ || playState == 1 || this.ˎ == null)) {
            this.ˎ.ॱ(this.ˊ, C.ˋ(this.ᐝ));
        }
        return true;
    }

    public int ˎ(long j) {
        return this.ˊ - ((int) (j - (ᐝ() * ((long) this.ˋ))));
    }

    public boolean ॱ(long j) {
        return this.ʼॱ != -9223372036854775807L && j > 0 && SystemClock.elapsedRealtime() - this.ʼॱ >= 200;
    }

    public void ˋ(long j) {
        this.ʾ = ᐝ();
        this.ˈ = SystemClock.elapsedRealtime() * 1000;
        this.ʿ = j;
    }

    public boolean ˊ(long j) {
        return j > ᐝ() || ʽ();
    }

    public boolean ˊ() {
        ॱॱ();
        if (this.ˈ != -9223372036854775807L) {
            return false;
        }
        ((acg) amm.ॱ(this.ʽ)).ˋ();
        return true;
    }

    public void ॱ() {
        ॱॱ();
        this.ॱ = null;
        this.ʽ = null;
    }

    private void ˎ() {
        long ʼ = ʼ();
        if (ʼ != 0) {
            long nanoTime = System.nanoTime() / 1000;
            if (nanoTime - this.ˋॱ >= 30000) {
                this.ˏ[this.ʻॱ] = ʼ - nanoTime;
                this.ʻॱ = (this.ʻॱ + 1) % 10;
                if (this.ʽॱ < 10) {
                    this.ʽॱ++;
                }
                this.ˋॱ = nanoTime;
                this.ʻ = 0;
                for (int i = 0; i < this.ʽॱ; i++) {
                    this.ʻ += this.ˏ[i] / ((long) this.ʽॱ);
                }
            }
            if (!this.ʼ) {
                ॱ(nanoTime, ʼ);
                ʼ(nanoTime);
            }
        }
    }

    private void ॱ(long j, long j2) {
        acg o_acg = (acg) amm.ॱ(this.ʽ);
        if (o_acg.ˋ(j)) {
            long ʼ = o_acg.ʼ();
            long ʽ = o_acg.ʽ();
            if (Math.abs(ʼ - j) > 5000000) {
                this.ˎ.ॱ(ʽ, ʼ, j, j2);
                o_acg.ˊ();
            } else if (Math.abs(ʽ(ʽ) - j2) > 5000000) {
                this.ˎ.ˋ(ʽ, ʼ, j, j2);
                o_acg.ˊ();
            } else {
                o_acg.ˎ();
            }
        }
    }

    private void ʼ(long j) {
        if (this.ˊॱ && this.ॱˊ != null && j - this.ॱˎ >= 500000) {
            try {
                this.ˏॱ = (((long) ((Integer) anr.ˎ((Integer) this.ॱˊ.invoke(amm.ॱ(this.ॱ), new Object[0]))).intValue()) * 1000) - this.ᐝ;
                this.ˏॱ = Math.max(this.ˏॱ, 0);
                if (this.ˏॱ > 5000000) {
                    this.ˎ.ˏ(this.ˏॱ);
                    this.ˏॱ = 0;
                }
            } catch (Exception e) {
                this.ॱˊ = null;
            }
            this.ॱˎ = j;
        }
    }

    private long ʽ(long j) {
        return (1000000 * j) / ((long) this.ॱॱ);
    }

    private void ॱॱ() {
        this.ʻ = 0;
        this.ʽॱ = 0;
        this.ʻॱ = 0;
        this.ˋॱ = 0;
    }

    private boolean ʽ() {
        return this.ʼ && ((AudioTrack) amm.ॱ(this.ॱ)).getPlayState() == 2 && ᐝ() == 0;
    }

    private static boolean ˊ(int i) {
        return anr.ˋ < 23 && (i == 5 || i == 6);
    }

    private long ʼ() {
        return ʽ(ᐝ());
    }

    private long ᐝ() {
        AudioTrack audioTrack = (AudioTrack) amm.ॱ(this.ॱ);
        if (this.ˈ != -9223372036854775807L) {
            return Math.min(this.ʿ, ((((SystemClock.elapsedRealtime() * 1000) - this.ˈ) * ((long) this.ॱॱ)) / 1000000) + this.ʾ);
        }
        int playState = audioTrack.getPlayState();
        if (playState == 1) {
            return 0;
        }
        long playbackHeadPosition = ((long) audioTrack.getPlaybackHeadPosition()) & 4294967295L;
        if (this.ʼ) {
            if (playState == 2 && playbackHeadPosition == 0) {
                this.ॱᐝ = this.ᐝॱ;
            }
            playbackHeadPosition += this.ॱᐝ;
        }
        if (anr.ˋ <= 28) {
            if (playbackHeadPosition == 0 && this.ᐝॱ > 0 && playState == 3) {
                if (this.ʼॱ == -9223372036854775807L) {
                    this.ʼॱ = SystemClock.elapsedRealtime();
                }
                return this.ᐝॱ;
            }
            this.ʼॱ = -9223372036854775807L;
        }
        if (this.ᐝॱ > playbackHeadPosition) {
            this.ॱˋ++;
        }
        this.ᐝॱ = playbackHeadPosition;
        return playbackHeadPosition + (this.ॱˋ << 32);
    }
}
